package com.example.myapplication.cp;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

public class MyContentProvider extends ContentProvider {
    public static final int BOOK_DIR = 0;
    public static final int BOOK_ITEM = 1;
    public static final int CATEGORY_DIR = 2;
    public static final int CATEGORY_ITEM = 3;
    public static final String AUTHORITY = "com.liaowei.cp.data";
    private static UriMatcher uriMatcher;
    private SQLiteDatabase db;

    static {
        // uriMatcher进⾏初始化，将期望匹配的集中uri加⼊
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY, "book", BOOK_DIR);
        uriMatcher.addURI(AUTHORITY, "book/#", BOOK_ITEM);
    }


    public MyContentProvider() {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)) {
            case BOOK_DIR:
                return "vnd.android.cursor.dir/vnd.com.liaowei.cp.data.book";
            case BOOK_ITEM:
                return "vnd.android.cursor.item/vnd.com.liaowei.cp.data.book";
        }
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int match = uriMatcher.match(uri);
        String table = null;
        switch (match) {
            case BOOK_DIR: table = "book";break;
        }
        // 向数据库中插入从contentResolver传来的数据,返回id
        Long id = db.insert(table, null, values);
        // 通过ContentUris获取插入有id之后的Uri
        Uri ReturnuUri = ContentUris.withAppendedId(uri, id);
        // 将id追加到uri后面
        Log.e("MyContentProvider", "执行了contentProvider的insert操作");

        // 通知更新
        getContext().getContentResolver().notifyChange(uri, null);

        // 返回给contentResolver
        return ReturnuUri;
    }

    @Override
    public boolean onCreate() {
        SQLiteOpenHelper helper = new SQLiteOpenHelper(getContext(), "book_store.db", null, 1) {
            @Override
            public void onCreate(SQLiteDatabase db) {
                // 创建数据库的表
                String sql = "create table book(_id integer primary key autoincrement," +
                        "name varchar(20)," +
                        "category varchar(4))";
//                db.execSQL(sql);
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            }
        };
        // 拿到读写数据库对象，如果没有数据库则创建，有则打开
        db = helper.getReadableDatabase();
        // 返回true 后面的方法才能被调用
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        int match = uriMatcher.match(uri);
        String table = null;

        switch (match) {
            case BOOK_DIR:
            case BOOK_ITEM:
                table = "book";break;
        }

        Cursor cursor = db.query(table,
                projection, // 指定在哪些列中查询，填null是查询所有
                selection, // 查询条件
                selectionArgs, // 查询条件值数组
                null, // 指定分组的列名
                null, // 去除不符合条件的列条件
                null, // 排序
                sortOrder);
        return cursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}